package com.authine.cloudpivot.web.api.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.authine.cloudpivot.web.api.dao.AppFunctionMapper;
import com.authine.cloudpivot.web.api.dao.CustomerdemandListMapper;
import com.authine.cloudpivot.web.api.dao.UserMapper;
import com.authine.cloudpivot.web.api.service.QuestionnaireService;
import com.authine.cloudpivot.web.api.vo.AppFunction;
import com.authine.cloudpivot.web.api.vo.CustomerdemandList;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Slf4j
@Service
public class QuestionnaireServiceImpl implements QuestionnaireService {

    @Autowired
    private AppFunctionMapper appFunctionMapper;

    @Autowired
    private FescoadQueryServiceImpl fescoadQueryService;

    @Autowired
    private CustomerdemandListMapper customerdemandListMapper;

    @Autowired
    private UserMapper userMapper;


    /**
     * @Description : 查询项目所有问卷
     * @param ： projectId
     * @author : Liun
     * @date : 2020/6/22
     */
    @Override
    public List<String> getAppFunctionNames(String projectNo){
        List<AppFunction> appFunctionList =  appFunctionMapper.selectAppFunction(projectNo);
        List<String> appFunctionNames=new ArrayList<>();
        if(ObjectUtil.isNotNull(appFunctionList)){
            for(AppFunction appFunction:appFunctionList){
                appFunctionNames.add(appFunction.getName());
            }
        }
        return appFunctionNames;
    }

    /**
     * @Description : 推送调查问卷消息通知
     * @param ： projectId
     * @param ： Questionnaire
     * @author : Liun
     * @date : 2020/6/22
     */
    @Override
    public  void remindQuestionnaire(String objectId,String Questionnaire,String storeRotation,String subjectNames){
        Example appFunctionExample=new Example(AppFunction.class);
        Example.Criteria appFunctionCriteria= appFunctionExample.createCriteria();
        appFunctionCriteria.andEqualTo("name",Questionnaire);
        AppFunction appFunction=appFunctionMapper.selectOneByExample(appFunctionExample);
        String url="";
        if(ObjectUtil.isNotNull(appFunction)){
            url = "form/detail?startWorkflowCode="+appFunction.getCode()+"&return=/workflow-center/start-workflow";
        }
        String receivers;
        String title="问卷调查";
        String content="你有《"+subjectNames+"》项目，店检轮次为："+storeRotation+"的问卷调查需要填写！";

        Example example=new Example(CustomerdemandList.class);
        Example.Criteria criteria= example.createCriteria();
        criteria.andEqualTo("parentid",objectId);
        List<CustomerdemandList> customerdemandLists=customerdemandListMapper.selectByExample(example);
        if(ObjectUtil.isNotNull(customerdemandLists)){
            for(CustomerdemandList customerdemandList:customerdemandLists){
                //发送消息通知
                String member= customerdemandList.getMember();
                List<Map> list1 = (List<Map>) JSON.parse(member);
                Map m1 = list1.get(0);
                receivers=(String) m1.get("id");
                receivers=userMapper.getUserId(receivers).getUserId();
                fescoadQueryService.sendMessage(receivers,url,content,title);
            }
        }


    }







}



